﻿@model Wheeler.Web.ViewModels.TestResultViewModel
@using Wheeler.Web.Utility;
@using Wheeler.Common.Extensions;

@{
    bool _HasSaved = WebUtil.GetQueryStringBoolean("IsSaved");
	String pageTitle = _HasSaved ? "Confirm and Save Data Sheet" : String.Format("Data Sheet {0}", Model != null && Model.Vehicle != null ? "of " + Model.Vehicle.RegistrationNo : String.Empty);    
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    bool _IsDataSheetPrintMode = String.Compare(WebUtil.GetQueryStringValue("ViewType"), "Print", true) == 0 ? true : false;
    
    if(_IsDataSheetPrintMode){
        Layout = "~/Views/Shared/_LayoutPrint.cshtml";
        ViewData[AppConstants.ViewDataKeys.IMAGE_UPLOAD_ERROR] = true;
    }
    else{
        if (UserInfo.IsAdmin) {
            ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = AdminMenu.EditTestResult;
        }
        else {
            ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = OperatorMenu.TestResults;
        }
    }

    String[] _PrintMargin = ConfigReader.DataSheetPrintMargin;    
}

<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    @if (!_IsDataSheetPrintMode) {
    <div>Review the Test Result and then hit Confirm button to Save this information.</div>
    }
</div>
<div class="jb-page-content">
@using (Html.BeginForm())
{
    if (Model == null)
    {
        @: @Html.WarningMessageBox("Sorry! Requested Data Sheet was not found.")
    }
    else
    {
        if (_HasSaved)
        {
            @Html.SuccessMessageBox("Congratulations! the Data Sheet saved successfully. <ul><li>Now review your given information by checking the final certificate.</li> <li>And Confirm this Data Sheet to mark as final.</li><li>If anything is wrong then Go Back & Edit</li></ul>")
        }
        else if (ViewData[AppConstants.ViewDataKeys.SAVED] != null && !_HasSaved)
        {
            @Html.WarningMessageBox("Sorry! we could not save the Data Sheet temporarily.")
        }
		@Html.HiddenFor(model => model.ID)
        @Html.HiddenFor(model => model.VehicleID)

        ViewData[AppConstants.ViewDataKeys.SECONDARY_MODEL] = true;

        @Html.Partial("_EditZGL", Model)
        if (!_IsDataSheetPrintMode) {
		<div class="jb-button-container hidden-print">            
			<button class="btn btn-primary@(Model.HasConfirmed ? " disabled" : "")" type="button" data-loading-text="Saving..." onclick="Confirm(@Model.ID, this);" @(Model.HasConfirmed ? "disabled" : "")><i class="icon-thumbs-up icon-white"></i> Confirm</button>
            @{
                                                                                                                                                        String canEditClass = UserInfo.CanEditTestResult(Model.HasConfirmed, Model.HasPublished) ? String.Empty : " disabled";
            }

            <a id="aPrintDataSheet" class="btn" href="javascript:void(0);" onclick="PrintDataSheet(@Model.ID, @Model.VehicleID)"><i class="icon-print"></i> Data Sheet</a>

            <a id="aEdit" class="btn@(canEditClass)" href="@(canEditClass == " disabled" ? "javascript:void(0);" : "/Private/TestResult/Edit/" + Model.ID + "?VehicleID=" + Model.VehicleID)"><i class="icon-arrow-left"></i> Go Back & Edit</a>

            <a id="aPrint" class="btn@(!Model.HasConfirmed ? " disabled" : "")" href="javascript:void(0);" onclick="PreviewCertificate(@Model.ID, @Model.VehicleID)"><i class="icon-print"></i> Certificate</a>
		</div>
        }
    }
}
</div>
@*Following styles should be changed in TestResult/Edit.cshtml also for consistency, if changed anything*@
@section PageHeadSection
{
    
    <style type="text/css">
        @*@@media print{@page {size: portrait;}}*@
        @@media print{@page {size: portrait; margin:0.2in;}}
        @if(_IsDataSheetPrintMode){
            <text>
             table.data-table-list td{padding:1px;}
             .data-table-list{ font-family:Adorsholipi, verdana; font-size:8pt;}
             label{font-size:8pt;}      
            </text>
        }
        
        label{display:inline;}
        table.data-table-list th{font-weight:bolder; color:#000000; background-color:#ffffff; text-align:center;}
        .data-table-list td.right{text-align:right;}
        .data-table-list td.center{text-align:center;}
        input[type="text"]{width:50px; padding-top:1px; padding-bottom:1px; margin:0px;}
        input[type="text"].large{width:auto; padding-top:1px; padding-bottom:1px; margin:0px;}
        div.spacer{margin:20px;}
                
    </style>
}
@section JavaScriptPlaceHolder
{
    <script language="javascript" type="text/javascript">
        var _HasConfirmed = @(Model.HasConfirmed ? "true" : "false");
        var _HasPublished = @(Model.HasPublished ? "true" : "false");
        var _PopupWidth = 1280, _PopupHeight = 600;
        function PreviewCertificate(id, vehicleID) {
            if(_HasConfirmed){
                var url = '/Private/TestResult/PublishDataSheet/@Model.ID';
                if(!_HasPublished){
                    ShowLoadingAnimation(false);
                    Ajax(url, null, function(hasPublished){
                        _HasPublished = true;
                        HideLoadingAnimation();
                    });
                }
                url = '/Private/TestResult/Print/' + id + '?VehicleID=' + vehicleID;
                ShowCenteredPopUp(url, 'PrintCertificate', _PopupWidth, _PopupHeight, true);
            }
        }

        function PrintDataSheet(id, vehicleID) {            
            url = '/Private/TestResult/Confirm/' + id + '?VehicleID=' + vehicleID + '&ViewType=Print';
            ShowCenteredPopUp(url, 'PrintDataSheet', _PopupWidth, _PopupHeight, true);
            
        }

        function Confirm(id, btn) {
            var url = '/Private/TestResult/ConfirmDataSheet/@Model.ID';
            ShowLoadingAnimation(false);
            Ajax(url, null, function(hasConfirmed){
                HideLoadingAnimation();
                if(hasConfirmed){
                    $(btn).attr("disabled", true).addClass('disabled');
                    _HasConfirmed = true;
                    $('#aPrint').removeClass('disabled');
                    $('#aEdit').addClass('disabled').attr('href', 'javascript:void(0);');
                }
            });
        }
    
    </script>

}

